const app = getApp()
import {policy} from '../../../../../api/oss'
const util = require('../../../../../utils/util.js')
Component({
  properties: {
    realNameForm:{
      type:Object
    }
  },
  data: {
    array: ['上传照片'],
    index: 0,
    previewImgs: [],
    images: [],
    previewVideo: [],
    video: [],
    dataObj: {
      policy: '',
      signature: '',
      key: '',
      ossaccessKeyId: '',
      dir: '',
      host: ''
    },
    host: "",
    metaType: 'img',
    payCode:'',
    uuid:'',
    fileType:''
  },
  lifetimes: {
    attached: function() {
      setTimeout(() => {
        this.setData({
          ['realNameForm.imgCardFront']: this.data.realNameForm.imgCardBack,
        })
      }, 2000);
    },
  },

  methods: {
    uploadClick() {
      this.bindPickerChange()
    },
    bindPickerChange: function(e) {
      this.cancelImgClick(e)
      this.setData({
        index: e.detail.value
      })
      this.beforeUpload()
      if (e.detail.value == 0) {
        this.updataClick()
      } else {
        this.updataVideoClick()
      }
    },
    updataClick() {
      let _this = this;
      _this.data.metaType = 'img'
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          var tempFilePath = res.tempFilePaths
          _this.uploadImg(tempFilePath)
        }
      })
    },
    cancelImgClick(data) {
      var _this = this
      this.data.images.splice(data.target.dataset.index, 1)
      this.data.metaData = this.data.images.concat(this.data.video);
      var metaData = {
        images: _this.data.images,
        video: _this.data.video,
      }
      _this.triggerEvent("metaClick", metaData)
    },
    cancelVideoClick(data) {
      var _this = this
      this.data.video.splice(data.target.dataset.index, 1)
      this.setData({
        video: this.data.video
      })
      this.data.metaData = this.data.images.concat(this.data.video);
      var metaData = {
        images: _this.data.images,
        video: _this.data.video,
      }
      _this.triggerEvent("metaClick", metaData)
    },
    updataVideoClick() {
      let _this = this;
      _this.data.metaType = 'video'
      var tempFilePath = []
      wx.chooseVideo({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success(res) {
          tempFilePath.push(res.tempFilePath)
          _this.uploadImg(tempFilePath)
        }
      })
    },
    beforeUpload(file) {
      let _self = this;
      _self.uuid = util.getUUID()
      policy().then(response => {
        _self.data.dataObj.policy = response.data.policy;
        _self.data.dataObj.signature = response.data.signature;
        _self.data.dataObj.ossaccessKeyId = response.data.accessKeyId;
        _self.data.dataObj.dir = response.data.dir;
        _self.data.dataObj.host = response.data.action;
        _self.data.host = response.data.host
      }).catch(err => {
        wx.showToast({
          icon: "none",
          title: err.content,
        })
      })
    },
    // 上传图片
    uploadImg(filePath) {
      let _this = this;
      _this.setData({
        uploadPercent: true
      })
      let {host} = this.data.dataObj;
      filePath.map((item) => {
        _this.uuid = util.getUUID()
        _this.data.fileType = item.substring(item.lastIndexOf('.'));
        _this.data.dataObj.key = _this.data.dataObj.dir + '/' + _this.uuid + _this.data.fileType;
        wx.uploadFile({
          url: host,
          filePath: item,
          name: 'file',
          formData: _this.data.dataObj,
          success: res => {
            if (res.statusCode === 200 || res.statusCode === 204) {
              _this.setData({
                uploadPercent: false
              });
              if (_this.data.metaType == 'img') {
                _this.data.images.push(_this.data.host + '/' + _this.data.dataObj.dir + '/' + _this.uuid  +  _this.data.fileType);
                _this.setData({
                  images: _this.data.images
                });
              } else if (_this.data.metaType == 'video') {
                _this.data.video.push(_this.data.host + '/' + _this.data.dataObj.dir + '/' + _this.uuid  +  _this.data.fileType);
                _this.setData({
                  video: _this.data.video
                });
              }
              _this.data.metaData = _this.data.images.concat(_this.data.video);
              var metaData = {
                images: _this.data.images,
                video: _this.data.video,
              }
              _this.triggerEvent("metaClick", metaData)
            }
          },
          fail: res => {
            wx.showToast({
              icon: "none",
              title: res.content,
            })
          }
        })
      });
    },
  }
})
